package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaCaseMaterial;
import com.lilosoft.business.cga.models.EgsProjectMaterials;
import com.lilosoft.business.precity.dao.UpProMaterialMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProMaterial;
import com.lilosoft.business.precity.models.UpProMaterialExample;
import com.lilosoft.core.utils.commons.StringUtils;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 材料目录信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 10:05
 */
@Service
public class UpProMaterialService {
    @Autowired
    private UpProMaterialMapper upProMaterialMapper;

    public UpProMaterial UpProMaterialSynchronous(UpProAccept upProAccept, CgaCaseMaterial cgaMaterial, EgsProjectMaterials egsMaterials) {
        UpProMaterial model = new UpProMaterial();
        BeanUtils.copyProperties(upProAccept, model);

        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());
        model.setTaskguid(cgaMaterial.getMaterialId());
        model.setMaterialname(egsMaterials.getMaterialName());
        model.setGettype("2");
        model.setMaterialProperties("0".equals(egsMaterials.getIsDeclare()) ? 1 : 2);

        if(StringUtils.isEmpty(cgaMaterial.getIsPass())){
            model.setMaterialState(2);
        }else{
            switch (cgaMaterial.getIsPass()) {
                case "0":
                case "2":
                    model.setMaterialState(5);
                    break;
                case "1":
                    model.setMaterialState(4);
                    break;
                default:
                    model.setMaterialState(2);
                    break;
            }
        }
        List<UpProMaterial> upProMaterials = selectByTaskguid(model.getTaskguid());
        if (null== upProMaterials || upProMaterials.size()<=0) {
            upProMaterialMapper.insertSelective(model);
        }
        //upProMaterialMapper.insertSelective(model);
        return model;

    }

    public List<UpProMaterial> selectByTaskguid(String taskguid){
        UpProMaterialExample example= new UpProMaterialExample();
        UpProMaterialExample.Criteria criteria = example.createCriteria();
        criteria.andTaskguidEqualTo(taskguid);
        criteria.andDelStateEqualTo(0);
        return upProMaterialMapper.selectByExample(example);
    }

    /**
     * 根据cdBatch 统计数据个数（材料目录信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch) {
        UpProMaterialExample example= new UpProMaterialExample();
        UpProMaterialExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProMaterialMapper.countByExample(example);
    }


}
